#region File Description
//-----------------------------------------------------------------------------
// SpinningTriangleControl.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Windows.Media;
using CharacterAnimationEditor;
using CakeAnimation;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using System;
#endregion

namespace WinFormsGraphicsDevice
{
    class XnaControl : GraphicsDeviceControl
    {
        Stopwatch m_Timer;
        public MainEditor m_Editor;
        public Context m_Context;

        protected override void Initialize()
        {
            Mouse.WindowHandle = Handle;
            //MouseWheel += new MouseEventHandler(XnaControl_MouseWheel);
            MouseEnter += new EventHandler(XnaControl_MouseEnter);

            m_Editor = new MainEditor();
            m_Timer = Stopwatch.StartNew();
            
            m_Context = new Context();
            m_Context.GD = GraphicsDevice;
            m_Context.Content = new ContentManager(Services);
            m_Context.Content.RootDirectory = "Content";

            m_Context.AnimationContext = new AnimationContext();
            m_Context.AnimationContext.Content = m_Context.Content;
            m_Context.AnimationContext.Sb = m_Context.SpriteBatch;


            m_Editor.LoadContent(m_Context);

            // Hook the idle event to constantly redraw our animation.
            CompositionTarget.Rendering += new System.EventHandler(CompositionTarget_Rendering);
        }

        void XnaControl_MouseEnter(object sender, EventArgs e)
        {
            Select();
            Focus();
        }

        const int WM_MOUSEWHEEL = 0x20A;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_MOUSEWHEEL)
                m_Context.MouseScroll = (int)m.WParam / Math.Abs((int)m.WParam);
        }

        void XnaControl_MouseWheel(object sender, MouseEventArgs e)
        {
            m_Context.MouseScroll = e.Delta / Math.Abs(e.Delta);
        }

        void CompositionTarget_Rendering(object sender, System.EventArgs e)
        {
            Invalidate();
        }


        protected override void Draw()
        {
            System.Drawing.Point p = PointToClient(new System.Drawing.Point(Left, Top));
            m_Context.WindowOffset = new Vector2(p.X, p.Y);

            m_Context.DeltaTime = (float)m_Timer.Elapsed.TotalSeconds;
            m_Timer.Reset();
            m_Timer.Start();
            m_Context.AnimationContext.DeltaTime = m_Context.DeltaTime;

            m_Editor.Update(m_Context);
            m_Editor.Draw(m_Context);

            m_Context.MouseScroll = 0;
        }

        public void Resize(int width, int height)
        {
            graphicsDeviceService.ResetDevice(width, height);
        }
    }
}
